$(function (){
    /*$("#back").click(function () {
        window.close();
    });*/
    $("#back").click(function () {
        if (window.confirm("确定取消支付?")){
            alert("取消成功");
            window.close();
        }
    });

    $("#pay").click(function (){
        var totalPriceVal = $("#totalPrice").val();
        var pIdVal = $("#pId").val();
        $.ajax({
            type: "GET",
            url: "buydrug/pay",
            data: {"pId": pIdVal},
            dataType: "json",
            success:function (result){
                if (result == "empty"){
                    alert("未办理就诊卡，请先办理就诊卡")
                    window.close();
                }else {
                    if (result.balance-totalPriceVal<0){
                        alert("余额不足，请先缴费");
                        window.location.href="buydrug/intorechargeablecard?pCard="+result.pCard+"&balance="+result.balance;
                    }
                    else {
                        $("#balance").val(result.balance-totalPriceVal);
                        paymoney();
                    }
                }
            },
            error: function (result) {
                alert("支付失败")
            }
        })
    });

    function paymoney(){
        var balanceVal = $("#balance").val();
        $.ajax({
            type: "GET",
            url: "buydrug/paymoney",
            data: {"balance": balanceVal},
            dataType: "json",
            success:function (result){
                //alert(result);
                if (result == "success"){
                    alert("缴费成功")
                    window.close();
                }else {
                    alert("缴费失败")
                }
            },
            error: function (result) {
                alert("缴费失败")
            }
        });
    }
});